<?php
    class product_edit_Controller extends controllerBase
    {
        public function process()
        {
            $this->aParams['product'] = $this->iId;
            $oDatabase = new database();
            $query ='PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX dc: <http://purl.org/dc/elements/1.1/>
            PREFIX bus: <'.URL::getLink('id/bussiness#').'>

            SELECT * 
            WHERE
            {
            ?uri rdf:type bus:product.
            ?uri bus:name ?name.
            ?uri bus:ownUser ?ownUser.  
            ?uri bus:description ?description.
            ?uri bus:image ?image.
            ?uri bus:hasManufacturer ?manufacturer.
            ?uri bus:price ?price.
            ?uri bus:hasCategory ?category.
            OPTIONAL{?uri bus:location ?location.}.
            FILTER (?uri = "'.URL::getLink('product/id/'.$this->iId).'") .
            }';

            $aProductEdit = $oDatabase->loadQuery($query);   
            $pEdit = $aProductEdit['result']['rows'][0];
            // Result
            $this->aParams['image'] = $pEdit['image'];
            $this->aParams['name'] = $pEdit['name'];
            $this->aParams['price'] = $pEdit['price'];
            $this->aParams['description'] = $pEdit['description'];
            $this->aParams['location'] = $pEdit['location'];
            $sQuery = 'select *from location ';
            $aLocations = $oDatabase->getListObject($sQuery,true,true);
            //            print_r($aLocations);
            $this->aParams['aLocations'] = $aLocations;

            $this->aParams['category'] = $pEdit['category'];// Get Name Category
            $sCategory ='PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX dc: <http://purl.org/dc/elements/1.1/>
            PREFIX bus: <'.URL::getLink('id/bussiness#').'>

            SELECT * 
            WHERE
            {
            ?uri rdf:type bus:category.
            ?uri bus:name ?name.
            ?uri bus:subCategory ?subCategory.
            ?uri bus:childCategoryOf ?childCategoryOf.
            FILTER (?uri = "'.$pEdit['category'].'") .
            }';
            $aCate = $oDatabase->loadQuery($sCategory);   
            $aCategory = $aCate['result']['rows'];
            $list_categories = '';
            $count =0 ;
            $this ->aParams['list_categories'] ='';
            if(count($aCategory))
            {
                foreach($aCategory as $cata)
                {
                    $count+=1;
                    $list_categories = $list_categories.$cata['childCategoryOf'];
                    if($count != count($aCategory))
                    {
                        $list_categories = $list_categories .';';

                    }
                }
            }
            $this ->aParams['list_categories'] = $list_categories;
            $this ->aParams['category_uri'] = '';
            $this ->aParams['category_name'] = '';
            if(isset($aCate['result']['rows'][0]['uri']))
            {
                $this ->aParams['category_uri'] = $aCate['result']['rows'][0]['uri'];
            }
            if(isset($aCate['result']['rows'][0]['name']))
            {
                $this ->aParams['category_name'] = $aCate['result']['rows'][0]['name'];
            }

            // Set Manufacture
            $sManquery = 'PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX dc: <http://purl.org/dc/elements/1.1/>
            PREFIX bus: <'.URL::getLink('id/bussiness#').'>

            SELECT * 
            WHERE
            {
            ?uri rdf:type bus:manufacturer.
            ?uri bus:name ?name.
            FILTER (?uri = "'.$pEdit['manufacturer'].'") .
            }';
            $aMan = $oDatabase->loadQuery($sManquery);   
            $pman = $aMan['result']['rows'][0];
            $this->aParams['manufacturer'] = $pman['name'];

            // Data Exit Product
            if(isset($_POST['title']))
            {   
                $this->aParams['title'] = $_POST['title'];

                // Edit upload location here
                $destination_path = getcwd().DIRECTORY_SEPARATOR;
                $result = 0;
                $file_name=$_FILES['avatar']['name'];
                $target_path = $destination_path . basename( $_FILES['avatar']['name']);
                $image = '';
                if(@move_uploaded_file($_FILES['avatar']['tmp_name'], "upload/".$_FILES["avatar"]["name"])) {
                    $image =$_FILES['avatar']['name'];
                }
                else
                {
                    $this->aParams['error'] = 'da co loi phat sinh';
                }
                echo '<script type="text/javascript">window.top.window.uploadComplete("'.$image.'");</script>';
            }
            $oDatabase = new database();
            $this->getManufacturer();
        }
        public function getManufacturer()
        {
            $oDatabase = new database();
            $squery = 'PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX dc: <http://purl.org/dc/elements/1.1/>
            PREFIX bus: <'.URL::getLink('id/bussiness#').'>

            SELECT * 
            WHERE
            {
            ?uri rdf:type bus:manufacturer.
            ?uri bus:name ?name. 
            }';
            $aManufacturers = $oDatabase->loadQuery($squery);   
            $this->aParams['aManufacturers']=$aManufacturers['result']['rows'];
        }
    }
?>
